define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'card/entity/index' + location.search,
                    add_url: 'card/entity/add',
                    /*edit_url: 'card/entity/edit',
                    del_url: 'card/entity/del',*/
                    multi_url: 'card/entity/multi',
                    import_url: 'card/entity/import',
                    table: 'card_entity',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                escape:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        //{field: 'card_kabaw_id', title: __('Card_kabaw_id')},
                        {field: 'logtype', title: __('Logtype'), operate: false},
                        {field: 'kabaw_name', title: __('Kabaw_name'), operate: 'LIKE'},
                        {field: 'kabaw_illustrate', title: __('Kabaw_illustrate'), operate: false},
                        {field: 'kabaw_fate', title: __('Kabaw_fate'), operate: false},
                        /* {field: 'entitysite.kabaw_all_cost', title: __('Kabaw_all_cost'), operate:'BETWEEN'},
                         {field: 'entitysite.kabaw_all_price', title: __('Kabaw_all_price'), operate:'BETWEEN'},*/
                        {field: 'number', title: __('Number'), operate: false},
                        //{field: 'transfer_num', title: __('Transfer_num')},
                        {field: 'number_from', title: __('Number_from'), operate: false},
                        {field: 'number_to', title: __('Number_to'), operate: false},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'printAll',
                                    title:'印刷',
                                    text: '印刷',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: '',
                                    confirm: '确定印刷吗？',
                                    url: function (row, column) {
                                        var url = 'card/entity/printAll?ids=' + row.id;
                                        return url;
                                    },
                                    visible:function (row) {
                                        if(row.status==1){
                                            return false;
                                        }
                                        return true;
                                    },
                                    success: function (data, ret) {
                                        console.log(ret)
                                        window.location.href=ret.url;

                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        top.Toastr.error(ret.msg);
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                },
                                {
                                    name: 'printAll',
                                    title:'导出数据',
                                    text: '导出数据',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: '',
                                    confirm: '确定导出数据吗？',
                                    url: function (row, column) {
                                        var url = 'card/entity/printAll?ids=' + row.id;
                                        return url;
                                    },
                                    visible:function (row) {
                                        if(row.status==0){
                                            return false;
                                        }
                                        return true;
                                    },
                                    success: function (data, ret) {
                                        console.log(ret)
                                        window.location.href=ret.url;

                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        top.Toastr.error(ret.msg);
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                },
                            ],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
